package org.starsource.forum.server.domain;

import java.io.Serializable;

import org.starsource.forum.client.domain.MessageObject;
import org.starsource.forum.client.domain.ResourceObject;
import org.starsource.forum.client.util.Assertion;

public class Resource implements HasId, Serializable {

	protected static org.apache.log4j.Logger logger = org.apache.log4j.LogManager
	.getLogger(Resource.class.getName());
	
	private static final long serialVersionUID = 4175923297272134208L;

	private int id;
	
	private ForeignKey<Message> message = new ForeignKey<Message>();
	
	private String url;
	
	private String thumbUrl;
	
	private int width;
	
	private int height;
	
	public Resource() {
		
	}
	
	public Resource(Message message, String url, String thumbUrl, int width, int height) {
		this.message.setObject(message);
		this.url = url;
		this.thumbUrl = thumbUrl;
		this.width = width;
		this.height = height;
	}
	
	public Resource(int id, Message message, String url, String thumbUrl, int width, int height) {
		this.id = id;
		this.message.setObject(message);
		this.url = url;
		this.thumbUrl = thumbUrl;
		this.width = width;
		this.height = height;
	}
	
	public boolean equals(Object obj) {
		try {
			Resource object = (Resource)obj;
			if (this.id != object.id) {
				logger.debug("this.id=" + this.id + " object.id=" + object.id);
				return false;
			}
			if (this.message.getId() != object.message.getId()) {
				logger.debug("this.message_id=" + this.message.getId() + " object.message_id=" + object.message.getId());
				return false;
			}
			if (!Assertion.isEqual(this.url, object.url)) {
				logger.debug("this.url=" + this.url + " object.url=" + object.url);
				return false;
			}

			if (!Assertion.isEqual(this.thumbUrl, object.thumbUrl)) {
				logger.debug("this.thumbUrl=" + this.thumbUrl + " object.thumbUrl=" + object.thumbUrl);
				return false;
			}

			if (this.width != object.width) {
				logger.debug("this.width=" + this.width + " object.width=" + object.width);
				return false;
			}

			if (this.height != object.height) {
				logger.debug("this.height=" + this.height + " object.height=" + object.height);
				return false;
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public int getId() {
		return this.id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public int getMessageId() {
		return this.message.getId();
	}
	
	public void setMessageId(int messageId) {
		this.message.setId(messageId);
	}
	public Message getMessage() {
		return this.message.getObject();
	}
	
	public void setMessage(Message message) {
		this.message.setObject(message);
	}
	
	public String getUrl() {
		return this.url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	
	public ResourceObject getResourceObject(MessageObject messageObject) {
		return new ResourceObject(this.id, messageObject, this.url, this.thumbUrl, this.width, this.height);
	}
	
	public String getThumbUrl() {
		return this.thumbUrl;
	}
	public void setThumbUrl(String thumbUrl) {
		this.thumbUrl = thumbUrl;
	}
	
	public int getWidth() {
		return this.width;
	}
	
	public void setWidth(int width) {
		this.width = width;
	}
	
	public int getHeight() {
		return this.height;
	}
	
	public void setHeight(int height) {
		this.height = height;
	}

}
